#include "ui_bofugui.h"
#include "bofugui.h"

QDir exe(QDir::currentPath());           //Path to Executable


/********************************************************************************************/

void BofuGui::on_importButton_clicked()
{
    int import = QMessageBox::question(this, tr("Importing folder"),
                                    tr("Be carefull.\n"
                                       "If you continue, all data in the choosen folder will be copied!\n Do you want to continue?"),
                                    QMessageBox::Yes | QMessageBox::No);
    if (import == QMessageBox::Yes) {
        QString sourceFolder = QFileDialog::getExistingDirectory(this, tr("choose import folder"), QDir::homePath());
        if (!sourceFolder.isEmpty()){
            QString command = "cp -R "+sourceFolder +" "+exe.absolutePath()+"/bofu-project/";
            commandexec = system(command.toAscii().data());
            ui->statusBar->showMessage(tr("Folder has been imported"),5000);
            QMessageBox::information(this, (QString::fromUtf8("Import folder")),
                                     QString::fromUtf8("<h2>Finished</h2>"
                                                       "<p>If u want to use it now, type in the name of the imported folder.</p>"));
        }
    } else if (import == QMessageBox::No) {
        //do nothing
    }



}

void BofuGui::on_actionDaten_importieren_triggered()
{
    on_importButton_clicked();
}

void BofuGui::on_action_Export_active_project_triggered()
{
    int exportfolder = QMessageBox::question(this, tr("Exporting folder"),
                                    tr("Be carefull.\n"
                                       "If you continue, all data in the choosen folder will be copied!\n Do you want to continue?"),
                                    QMessageBox::Yes | QMessageBox::No);
    if (exportfolder == QMessageBox::Yes) {
        QString destinationFolder = QFileDialog::getExistingDirectory(this, tr("choose destination folder"), QDir::homePath());
        if (!destinationFolder.isEmpty()){


            QString command = "cp -R "+dir.absolutePath()+" "+destinationFolder;
            commandexec = system(command.toAscii().data());
            ui->statusBar->showMessage(tr("Folder has been exported"),5000);

            QMessageBox::information(this, (QString::fromUtf8("Export folder")),
                                     QString::fromUtf8("<h2>Finished</h2>"
                                                       "<p>Please check the destination folder!</p>"));
        }
    } else if (exportfolder == QMessageBox::No) {
        //do nothing
    }
}


/********************************************************************************************/

void BofuGui::enableSafeButton(const QString &text)
{
    ui->korpusNameButton->setEnabled(!text.isEmpty());
}

void BofuGui::on_korpusNameButton_clicked()
{
    korpusname=ui->lineEdit->text();
    dir = ( (exe.absolutePath()+"/bofu-project/"+korpusname).toAscii().data() );
    QString command = "mkdir "+dir.absolutePath();
    if ( !dir.exists() ){
        ui->statusBar->showMessage(tr("Corpus name has been set and a folder was created"),5000);
        commandexec = system(command.toAscii().data());
    } else{
        //qWarning( "Folder already exists" );
        ui->statusBar->showMessage(tr("Corpus name has been set, using existing folder"),5000);
    }

    command = "mkdir "+dir.absolutePath()+"/proto";
    QDir proto( (dir.absolutePath()+"/proto").toAscii().data() );
    if ( !proto.exists() ){
        commandexec = system(command.toAscii().data());
    } else{
        //qWarning( "Folder already exists" );
    }

    command = "mkdir "+dir.absolutePath()+"/proto/traindata";
    QDir traindata( (dir.absolutePath()+"/proto/traindata").toAscii().data() );
    if ( !traindata.exists() ){
        commandexec = system(command.toAscii().data());
    } else{
        //qWarning( "Folder already exists" );
    }

    command = "mkdir "+dir.absolutePath()+"/wav";
    QDir wav( (dir.absolutePath()+"/wav").toAscii().data() );
    if ( !wav.exists() ){
        commandexec = system(command.toAscii().data());
    } else{
        //qWarning( "Folder already exists" );
    }

    command = "mkdir "+dir.absolutePath()+"/boss_sampa";
    QDir boss_sampa( (dir.absolutePath()+"/boss_sampa").toAscii().data() );
    if ( !boss_sampa.exists() ){
        commandexec = system(command.toAscii().data());
    } else{
        //qWarning( "Folder already exists" );
    }

    command = "mkdir "+dir.absolutePath()+"/xml";
    QDir xml( (dir.absolutePath()+"/xml").toAscii().data() );
    if ( !xml.exists() ){
        commandexec = system(command.toAscii().data());
    } else{
        //qWarning( "Folder already exists" );
    }

    command = "mkdir "+dir.absolutePath()+"/sql";
    QDir sql( (dir.absolutePath()+"/sql").toAscii().data() );
    if ( !sql.exists() ){
        commandexec = system(command.toAscii().data());
    } else{
        //qDebug( "Folder already exists" );
    }

    command = "mkdir "+dir.absolutePath()+"/txt";
    QDir txt( (dir.absolutePath()+"/txt").toAscii().data() );
    if ( !txt.exists() ){
        commandexec = system(command.toAscii().data());
    } else{
        //qDebug( "Folder already exists" );
    }

    command = "mkdir "+dir.absolutePath()+"/synth";
    QDir synth( (dir.absolutePath()+"/synth").toAscii().data() );
    if ( !synth.exists() ){
        commandexec = system(command.toAscii().data());
    } else{
        //qWarning( "Folder already exists" );
    }

    command = "mkdir "+dir.absolutePath()+"/lab";
    QDir lab( (dir.absolutePath()+"/lab").toAscii().data() );
    if ( !lab.exists() ){

        commandexec = system(command.toAscii().data());
    } else{
        //qWarning( "Folder already exists" );
    }
    dir = ( (dir.absolutePath()).toAscii().data() );

    command = "touch "+dir.absolutePath()+"/"+korpusname+".txt "+dir.absolutePath()+"/"+korpusname+".log";
    commandexec = system(command.toAscii().data());


    if (ui->guidedBox->isChecked()){
        guided();       //start guided tour
    } else{
        enableAll();
    }

    //Read Log-File
    QFile log(dir.absolutePath()+"/"+korpusname+".log");
    if(!log.open(QFile::ReadOnly | QFile::Text))
        return;
    QTextStream in(&log);
      ui->logText->setPlainText(in.readAll());
    log.close();
    ui->pushButton_DeleteLog->setEnabled(1);
    ui->pushButton_openLog->setEnabled(1);

    //Read Diary-File
    QFile diary(dir.absolutePath()+"/"+korpusname+".txt");
    if(!diary.open(QFile::ReadOnly | QFile::Text))
        return;
    QTextStream diary_in(&diary);
    ui->plainTextEdit_Diary->setPlainText(diary_in.readAll());
    diary.close();

    check_existing_files();
}


void BofuGui::on_newButton_clicked()
{
    QDir::setCurrent((exe.absolutePath()));
    ui->guidedBox->setChecked(false);
    ui->action_Export_active_project->setEnabled(0);
    ui->korpusNameButton->setEnabled(1);
    ui->lineEdit->setEnabled(1);
    ui->newButton->setEnabled(0);
    ui->pushButton_importTranscription->setEnabled(0);
    ui->lexiconEdit->setText("");
    ui->tabWidget->insertTab(0, ui->tab_welcome, "Welcome"); // The tab tab_welcome is added back.
    //Remove all other tabs
    ui->tabWidget->removeTab(8);
    ui->tabWidget->removeTab(7);
    ui->tabWidget->removeTab(6);
    ui->tabWidget->removeTab(5);
    ui->tabWidget->removeTab(4);
    ui->tabWidget->removeTab(3);
    ui->tabWidget->removeTab(2);
    ui->tabWidget->removeTab(1);

    ui->guidedBox->setEnabled(false);

    ui->plainTextEdit_Diary->setPlainText("");
    lexfile = "";


    //ui->lineEdit->setText("");
    ui->lineEdit_orthographicFile->setText("");
    ui->lineEdit_transcriptionImport->setText("");
    ui->lineEdit_unitlist->setText("");
    ui->lineEdit_list->setText("");
    ui->lineEdit_wavFolder->setText("");
    ui->lineEdit_labelfiles->setText("");
    ui->lineEdit_labelfiles_2->setText("");
    ui->lineEdit->setFocus();
    ui->pushButton_createFileList->setEnabled(0);
    ui->deleteButton->setEnabled(0);
    ui->showFolderButton->setEnabled(0);
    ui->correctBox->setEnabled(0);
    ui->pushButton_importOrthographic->setEnabled(0);
    ui->pushButton_importTranscription->setEnabled(0);
    ui->pushButton_importUnitlist->setEnabled(0);
    ui->lexiconLoadButton->setEnabled(0);
    ui->pushButton_importFileList->setEnabled(0);
    ui->pushButton_importTranscription->setEnabled(0);
    ui->transcribeButton->setEnabled(0);
    ui->pushButton_loadWav->setEnabled(0);
    ui->exportButton->setEnabled(0);
    ui->labelimportButton->setEnabled(0);
    ui->labelimportButton_2->setEnabled(0);
    ui->codingButton->setEnabled(0);
    ui->pushButton_createDict->setEnabled(0);
    ui->wav2rawButton->setEnabled(0);
    ui->raw2wavButton->setEnabled(0);
    ui->lab2blfButton->setEnabled(0);
    ui->blf2xmlButton->setEnabled(0);
    ui->addHPButton->setEnabled(0);
    ui->optBoundsButton->setEnabled(0);
    ui->addPhrasesButton->setEnabled(0);
    ui->alignFlagButton->setEnabled(0);
    ui->addmelboundsButton->setEnabled(0);
    ui->editContextButton->setEnabled(0);
    ui->addContextButton->setEnabled(0);
    ui->decodingButton->setEnabled(0);
    ui->getF0BossButton->setEnabled(0);
    ui->addF0Button->setEnabled(0);
    ui->pushButton_DeleteLog->setEnabled(0);
    ui->pushButton_openLog->setEnabled(0);
    ui->pushButton_refreshfolder->setEnabled(0);

    dir = ( ("./bofu-project/") );
    model->setRootPath(dir.path());

    ui->checkBox_Transcription->setChecked(false);
    ui->checkBox_Unitlist->setChecked(false);
    ui->checkBox_Dict->setChecked(false);
    ui->checkBox_Convert->setChecked(false);
    ui->checkBox_Filelist->setChecked(false);

    ui->pushButton_showLog->setEnabled(0);
    commandexec = system("clear"); //cleaning terminal

    readSettings();
}

void BofuGui::on_showFolderButton_clicked()
{
    openURL(QUrl( dir.path() ));
}

void BofuGui::on_deleteButton_clicked()
{
    int del = QMessageBox::question(this, tr("Deleting folder"),
                                    tr("Are you sure that you want to delete this folder?.\n"
                                       "If you continue, all data will be lost"),
                                    QMessageBox::Yes | QMessageBox::No);
    if (del == QMessageBox::Yes) {
        ui->statusBar->showMessage(tr("Deleting folder...please wait!!!"),5000);
        command = "cd ./bofu-project/; rm -r " +korpusname;
        commandexec = system(command.toAscii().data());
        QMessageBox::information(this, (QString::fromUtf8("Information")),
                                 QString::fromUtf8("<h2>Folder deleted</h2>"
                                                   "<p>The folder, all subfolders and data have been removed!"));
        on_newButton_clicked();

    } else if (del == QMessageBox::No) {
    }
    ui->logText->clear();
}

